import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:leader_help/page/task/main/models/area/area.dart';
import 'package:leader_help/page/task/main/models/area/area_list.dart';
import 'package:leader_help/page/task/main/models/area/area_item.dart';
import 'package:leader_help/page/task/main/models/industry_model/industry_model.dart';
import 'package:leader_help/utils/util.dart';

Future showIndustryPickers({
  required List<IndustryModel> mds,
  required IndustrySelectedModel selectedMod,
  required Function(IndustrySelectedModel) onChoosed,
  double left = 0,
  double top = 0,
  double right = 0,
  double height = 200,
  bool needBarrierColor = false,
}) async {
  await Get.dialog(
    Padding(
      padding: EdgeInsets.fromLTRB(left, top, right, 0),
      child: Column(
        children: [
          SizedBox(
            height: height,
            child: IndustryPickers(
              mods: mds,
              selectedMod: selectedMod,
              onChoosed: onChoosed,
            ),
          )
        ],
      ),
    ),
    barrierDismissible: true,
    barrierColor: needBarrierColor ? null : Colors.white.withAlpha(0),
  );
}

class IndustryPickers extends StatefulWidget {
  const IndustryPickers(
      {Key? key,
      required this.mods,
      required this.selectedMod,
      required this.onChoosed})
      : super(key: key);
  final List<IndustryModel> mods;
  final IndustrySelectedModel selectedMod;
  final Function(IndustrySelectedModel) onChoosed;
  @override
  State<IndustryPickers> createState() => _IndustryPickersState();
}

class _IndustryPickersState extends State<IndustryPickers> {
  late IndustrySelectedModel selectedMod;
  final List<String> types = ['第一产业(农业)', '第二产业(工业)', '第三产业(服务业)'];
  late List<IndustryModel> mods;

  @override
  void initState() {
    super.initState();
    mods = widget.mods;
    selectedMod = widget.selectedMod;
  }

  @override
  Widget build(BuildContext context) {
    var _items = mods[selectedMod.titleIndex].children;
    return Container(
        decoration: BoxDecoration(
            color: ColorsUtil.white,
            borderRadius: BorderRadius.vertical(bottom: Radius.circular(5.r))),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              height: 30.h,
              alignment: Alignment.centerLeft,
              padding: EdgeInsets.only(left: 10.r),
              child: Text(
                '按产业大类筛选',
                style: pfSCStyle(color: ColorsUtil.title, fontSize: 15.sp),
              ),
            ),
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 10.r),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: types
                    .asMap()
                    .map((key, value) => MapEntry(
                          key,
                          InkWell(
                            onTap: () {
                              selectedMod.bigIndex = key;
                              setState(() {});
                            },
                            child: Container(
                              padding: EdgeInsets.symmetric(
                                  horizontal: 6.r, vertical: 2),
                              decoration: BoxDecoration(
                                color: selectedMod.bigIndex == key
                                    ? const Color(0xffF5A79F)
                                    : ColorsUtil.textBg,
                                borderRadius: BorderRadius.circular(3),
                              ),
                              child: Text(
                                value,
                                style: pfMediumStyle(
                                    color: selectedMod.bigIndex == key
                                        ? Colors.white
                                        : ColorsUtil.title,
                                    fontSize: 13.sp),
                              ),
                            ),
                          ),
                        ))
                    .values
                    .toList(),
              ),
            ),
            Container(
              height: 30.h,
              alignment: Alignment.centerLeft,
              padding: EdgeInsets.only(left: 10.r),
              margin: EdgeInsets.only(top: 5.h),
              decoration: const BoxDecoration(
                  border: Border(
                bottom: BorderSide(color: ColorsUtil.separator, width: 0.5),
                top: BorderSide(color: ColorsUtil.separator, width: 0.5),
              )),
              child: Text(
                '按行业细分筛选',
                style: pfSCStyle(color: ColorsUtil.title, fontSize: 15.sp),
              ),
            ),
            Expanded(
              child: Row(
                children: [
                  SizedBox(
                    width: 120.w,
                    child: ListView.builder(
                      itemCount: mods.length,
                      itemBuilder: (context, index) {
                        var md = mods[index];
                        return InkWell(
                          onTap: () {
                            selectedMod.titleIndex = index;
                            selectedMod.titleMod = md;
                            setState(() {});
                          },
                          child: Container(
                            height: 50.h,
                            color: selectedMod.titleIndex == index
                                ? Colors.white
                                : ColorsUtil.bg,
                            child: Row(
                              children: [
                                selectedMod.titleIndex == index
                                    ? Container(
                                        width: 4.r,
                                        height: 12.h,
                                        color: ColorsUtil.red,
                                      )
                                    : const SizedBox(),
                                SizedBox(
                                  width: selectedMod.titleIndex == index
                                      ? 6.r
                                      : 10.r,
                                ),
                                Flexible(
                                  child: Text(
                                    md.name!,
                                    style: TextStyle(
                                        color: ColorsUtil.title,
                                        fontSize: 14.sp),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        );
                      },
                    ),
                  ),
                  Expanded(
                    child: ListView.builder(
                      itemCount: _items?.length ?? 0,
                      itemBuilder: (context, index) {
                        var md = _items![index];
                        return InkWell(
                          onTap: () {
                            selectedMod.index = index;
                            selectedMod.item = md;
                            widget.onChoosed(selectedMod);
                            Get.back();
                          },
                          child: Container(
                            height: 50.h,
                            padding: EdgeInsets.symmetric(horizontal: 10.r),
                            decoration: const BoxDecoration(
                              color: Colors.white,
                              border: Border(
                                left: BorderSide(
                                    color: ColorsUtil.separator, width: 0.5),
                              ),
                            ),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Flexible(
                                  child: Text(
                                    md.name!,
                                    maxLines: 3,
                                    style: TextStyle(
                                        color: selectedMod.index == index
                                            ? ColorsUtil.red
                                            : ColorsUtil.title,
                                        fontSize: 14.sp),
                                  ),
                                ),
                                selectedMod.index == index
                                    ? Icon(
                                        Icons.check,
                                        size: 16.r,
                                        color: ColorsUtil.red,
                                      )
                                    : const SizedBox(),
                              ],
                            ),
                          ),
                        );
                      },
                    ),
                  ),
                ],
              ),
            ),
          ],
        ));
  }
}

class IndustrySelectedModel {
  int bigIndex = 0;
  int titleIndex = 0;
  int index = -1;
  IndustryModel? titleMod;
  IndustryModel? item;
}
